<?php
namespace app\passport\model;
use think\Model;
use think\Session;
use think\Loader;
use think\Db;
use think\Config;
use think\helper\Time;

//业务模型

class Passport extends Model
{
    protected $autoWriteTimestamp = false;
    
    public function register($bin)
    {
        /**
         * 普通注册
         * return 如果失败,返回false 如果成功 返回id
         */
        

        //数据初始化
        $data   =   [
            'email'     =>  $bin['email'],
            'telphone'  =>  $bin['telphone'],
            'password'  =>  $bin['password'],
            
        ];
        //判断是否需要激活?
        $data['status'] =   1;//需要激活

        //新增,判断是否成功
        if(!$this->data($data)->save())
        {
            return false;
        }
        //如果需要激活,那么启动激活相关程序[ex:发送激活邮件,短信]
        
        //用户注册任务节点
        return  $this->id;
    }

    public function login($method,$bin)
    {
        $_method = ['email','telphone'];
        if(!in_array($method,$_method)){
            $this->error = '登录方式被禁止!';
            return false;
        }
        /**
         * 大众化登录方式,登录方式,登录账号,登录密码
         * 
         */
        //数据处理
        $data = [
            'email' =>  $bin['email'],
            'password' =>  $bin['password'],
        ];

        //把该用户的信息取出来,找不到信息返回错误,进行相关判断
        
        $user = $this->where('email', $data['email'])->find();
        
        //dump($this->getLastSql());
        //return $user;
        //判断密码是否正确
        if( empty($user) || $user->status == 0 || $user->password != $this->setPasswordAttr($data['password'])){
            if(empty($user)){
                $this->error = '登录账号不存在!';
            } elseif ($user->status == 0){
                $this->error = '用户被禁止登录!';
            } elseif ($user->password != $this->setPasswordAttr($data['password'])){
                $this->error = '密码错误!';
            }
            //写入日志
            //返回错误
            return false;
        }

        //判断其他用户状态,是否被冻结等

        //hash生成
        $hash = \app\common\tools\Strings::passHash($user->id);
        //积分操作,以后归于[任务节点]
        list($startTime, $endTime) = Time::yesterday();
        if($user->current_time<=$endTime)
        {
            //今天还没有登录,所以积分+1
            $user->score1 +=1;
            $loginTask = 1;
        }
        //[任务节点]结束
        
        //更新一些数据,还未save
        $user->last_time    =   $user->current_time;
        $user->last_ip      =   $user->current_ip;
        $user->current_time =   (int) THINK_START_TIME;
        $user->current_ip   =   \app\common\tools\Visitor::getIP();
        $user->hash         =   $hash;

        //处理,存session
        $sessionData = $user->data;
        unset($sessionData['password']);
        //把hash写入cookie;保存数据到session中
        \think\Cookie::set('hash',$hash);
        Session::set(Config::get('passport_identifier'),$sessionData);
        //更新数据到数据库,并启动登录相关任务(如增加积分)
        if(!$user->save()){
            //这里需要抛出错误,而不是用跳转错误
            $this->error = '读写数据库出错!';
            return false;
        }
        //积分改动日志,以后日志类都会调整
        if(isset($loginTask) && $loginTask){
            Loader::model('Log')->score('{s1+1}{sc='.$user->score1.'}');
        }
        //写入日志,需要辨别方式
        Loader::model('Log')->record('{login}');
        return $sessionData;

    }

    protected function setPasswordAttr($password)
    {
        //密码处理,比如登录的时候要处理
        return $password;
        return md5($password);
    }


}